package server;


public class MainServer {

	public static void main(String[] args) {
		int portNumber = 1500;
		String hostname;
		String address = "localhost";
		int idx;
		String outAddress = "";
		int outPortNumber = portNumber;
		int minNumberOfClient = -1;
		int maxNumberOfClient = -1;

		switch (args.length) {
		case 4:
			try {
				minNumberOfClient = Integer.parseInt(args[3]);
			} catch (Exception e) {
				System.out
						.println("Usage is: > java Server [Address]:<portNumber> [outServerAddress]:<outServerPortNumber> <maxNumberOfClient> <minNumberOfClient>");
				return;
			}
		case 3:
			try {
				maxNumberOfClient = Integer.parseInt(args[2]);
			} catch (Exception e) {
				System.out
						.println("Usage is: > java Server [Address]:<portNumber> [outServerAddress]:<outServerPortNumber> <maxNumberOfClient> <minNumberOfClient>");
				return;
			}

		case 2:
			try {
				hostname = args[1];
				idx = hostname.indexOf(":");
				if (idx > 0) {
					outAddress = hostname.substring(0, idx);
					outPortNumber = Integer.parseInt(hostname.substring(idx + 1));
				} else {
					maxNumberOfClient = Integer.parseInt(args[1]);
					// outAddress = hostname;
				}
			} catch (Exception e) {
				System.out.println("Invalid arguments.");
				System.out
						.println("Usage is: > java Server [Address]:<portNumber> [outServerAddress]:<outServerPortNumber> <maxNumberOfClient> <minNumberOfClient>");
				return;
			}
		case 1:
			try {
				hostname = args[0];
				idx = hostname.indexOf(":");
				if (idx > 0) {
					address = hostname.substring(0, idx);
					portNumber = Integer.parseInt(hostname.substring(idx + 1));
				} else {
					new Exception();
				}
			} catch (Exception e) {
				System.out.println("Invalid arguments.");
				System.out.println("Usage is: > java Server [Address]:<portNumber>");
				return;
			}
		case 0:
			System.out.println(maxNumberOfClient + " " + minNumberOfClient);
			if (!outAddress.equalsIgnoreCase("")) {
				Server server;
				if (maxNumberOfClient > 0) {
					if (minNumberOfClient > -1) {
						
						server = new Server(portNumber, address, outPortNumber,
								outAddress, maxNumberOfClient, minNumberOfClient);
						
					} else {
						server = new Server(portNumber, address, outPortNumber,
								outAddress, maxNumberOfClient);
					}
				} else {
					server = new Server(portNumber, address, outPortNumber, outAddress);
				}
				try {
					server.start();
				} catch (Exception e) {
				}
			} else if (maxNumberOfClient > 0) {
				Server server = new Server(portNumber, address, maxNumberOfClient);
				try {
					server.start();
				} catch (Exception e) {
				}
			} else {
				Server server = new Server(portNumber, address);
				try {
					server.start();
				} catch (Exception e) {
				}
			}
			break;
		default:
			System.out.println("Usage is: > java Server [portNumber]");
			return;
		}
	}
}
